#include "showpiclabel.h"
#include <QWidget>
#include <QPainter>

extern unsigned short tar_x,tar_y;
extern unsigned char mubiaodiushi;
extern int TmpW,TmpH;//模板宽度、模板高度
extern unsigned char *Template;
extern unsigned char *show_16x16;
extern unsigned char *scale_2_moban;

showPicLabel::showPicLabel(QWidget *parent) : QLabel(parent)
{

    setText("hello pic");
}

int showPicLabel::showPic(QImage img)
{
    pix.convertFromImage(img);

    QImage moban =  QImage(Template, TmpW, TmpH, QImage::Format_Indexed8);
    QImage moban2 =  QImage(show_16x16, TmpW, TmpH, QImage::Format_Indexed8);
    QImage moban3 =  QImage(scale_2_moban, 32, 32, QImage::Format_Indexed8);
    moban_pix.convertFromImage(moban);
    moban_pix2.convertFromImage(moban2);
    moban_pix3.convertFromImage(moban3);
//    this->setPixmap(pix);
//    this->setAlignment(Qt::AlignCenter);

    short xishuPic = pix.width() / pix.height();
    short xishuLabel = this->width() / this->height();
    showPicW = 0;
    showPicH = 0;
    startPicX = 0;
    startPicY = 0;

    if(xishuPic > xishuLabel)
    {
        showPicW = this->width();
        showPicH = this->width() / xishuPic;
        startPicX = 0;
        startPicY = (this->height() - showPicH) / 2;
    }
    else if(xishuPic < xishuLabel)
    {
        showPicW = this->height() * xishuPic;
        showPicH = this->height();
        startPicX = (this->width() - showPicW) / 2;
        startPicY = 0;
    }
    else
    {
        showPicW = this->width();
        showPicH = this->height();
        startPicX = 0;
        startPicY = 0;
    }

    update();
}

void showPicLabel::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);
    QPainter painter(this);

    // 反走样
    painter.setRenderHint(QPainter::Antialiasing, true);


    QRect rect(startPicX, startPicY, showPicW, showPicH);
    QRect moban_rect(0, 0, 64, 64);
    QRect moban_rect2(0, 100, 64, 64);
    QRect moban_rect3(0, 200, 64, 64);

    // 绘制图
    painter.drawPixmap(rect, pix);
    // 绘制图
    painter.drawPixmap(moban_rect, moban_pix);
    painter.drawPixmap(moban_rect2, moban_pix2);
    painter.drawPixmap(moban_rect3, moban_pix3);
    if(!mubiaodiushi)
    {
        painter.setPen(QPen(Qt::red,1));//设置画笔形式

        double xx = (double)(tar_x - TmpW / 2) / (double)2048.0;
        double yy = (double)(tar_y - TmpW / 2) / (double)2048.0;

        int rect_x = xx * showPicW + startPicX;
        int rect_y = yy * showPicH + startPicY;

        double ww = TmpW / 2048.0;
        double hh = TmpH / 2048.0;
        int www = ww * showPicW;
        int hhh = hh * showPicH;

        painter.drawRect((int)rect_x, (int)rect_y, www, hhh);//画矩形
    }
}
